package model

import (
	"fmt"
	"gitee.com/go-package/carbon"
	"time"
)

type SmsCode struct {
	Id     int    `json:"id"`
	Mobile string `json:"mobile"`
	BizId  string `json:"biz_id"`
	Status int    `json:"status"`
	Code   string `json:"code"`
	Ctime  int64  `json:"ctime"`
}

//用户注册插入数据
func InsertSmsCode(mobile string, status int, bizid string, code string) bool {
	smsCode := SmsCode{}
	smsCode.Mobile = mobile
	smsCode.Ctime = time.Now().Unix()
	smsCode.BizId = bizid
	smsCode.Status = status
	smsCode.Code = code
	err := DB.Table("sms_code").Create(&smsCode).Error
	if err != nil {
		fmt.Println(err.Error())
		return false
	}
	return true
}

// 发送短信次数限制 6个小时内 6个电话
func SendMsgImposeNum(mobile string) bool {
	smsCode := make([]SmsCode, 0)
	car := carbon.Now()
	fmtTime := car.Now().SubHours(6).ToDateTimeString()
	startTime := car.Parse(fmtTime).ToTimestamp()
	endTime := time.Now().Unix()
	err := DB.Table("sms_code").Where("mobile = ? AND ctime >= ? AND ctime <= ?", mobile, startTime, endTime).Find(&smsCode).Error
	if err != nil || len(smsCode) > 6 {
		return false
	}
	return true
}
